<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="活动名称"></el-input>
      </el-form-item>
      <el-form-item label="活动地点" prop="address">
        <el-input v-model="dataForm.address" placeholder="活动地点"></el-input>
      </el-form-item>
      <el-form-item label="活动时间" prop="activitytime">
        <el-date-picker
          v-model="dataForm.activitytime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import activityVue from "./activity.vue";
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        }
      },
      visible: false,
      dataForm: {
        actId: 0,
        name: "",
        address: "",
        updatetime: "",
        activitytime: "",
        state: "",
        createtime: ""
      },
      dataRule: {
        name: [
          { required: true, message: "活动名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "活动地点不能为空", trigger: "blur" }
        ],
        activitytime: [
          { required: true, message: "活动时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.actId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.actId) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/activity/info/${this.dataForm.actId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.activity.name;
              this.dataForm.address = data.activity.address;
              this.dataForm.updatetime = data.activity.updatetime;
              this.dataForm.activitytime = data.activity.activitytime;
              this.dataForm.state = data.activity.state;
              this.dataForm.createtime = data.activity.createtime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/activity/${!this.dataForm.actId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              actId: this.dataForm.actId || undefined,
              name: this.dataForm.name,
              address: this.dataForm.address,
              updatetime: this.dataForm.updatetime,
              activitytime: this.dataForm.activitytime,
              state: this.dataForm.state,
              createtime: this.dataForm.createtime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              console.log("错误消息", data.data);
              if (data.data.activitytime) {
                this.$message.error(data.data.activitytime);
              } else if(data.data.name){
                this.$message.error(data.data.name);
              }else if(data.data.address){
                this.$message.error(data.data.address);
              }else{
                this.$message.error(data.msg);
              }
            }
          });
        }
      });
    }
  }
};
</script>
